from flask import Blueprint, jsonify, request
from pydantic import ValidationError

from cfganalysis.ProgramAnalysis import generate_cfg, generate_cfg_with_gpt
from utils.parseData import response_json


python_bp = Blueprint('python', __name__)

# 注册加速度接口
@python_bp.route('/analyze', methods=['POST'])
def analyze_cprogram():

    # 从请求中获取 JSON 数据
    try:
        data = request.json
        code = data.get("code", "")
        language = data.get("language","")
        model = data.get("model","gpt-3.5-turbo")
        if not code:
            response_json(514, "代码不能为空")  # 返回验证错误
        return generate_cfg_with_gpt(code,language,model)
    except (ValidationError, TypeError) as e:
        return response_json(503, str(e))# 返回验证错误



